﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Volume_Resource_Management
{
    public partial class frmFirstLoad : Form
    {
        int[,] newMap = new int[251, 118];
        List<List<string>> weeklyInfo = new List<List<string>>();

        List<int> butLocs = new List<int>();

        string storeNo = "";
        frmSplash splash;
        int currentYear;

        Label[] ter1 = new Label[18];
        Label[] ter2 = new Label[18];
        Label[] ter3 = new Label[18];

        TextBox[] ter1TB = new TextBox[18];
        TextBox[] ter2TB = new TextBox[18];
        TextBox[] ter3TB = new TextBox[18];

        public frmFirstLoad(string sNo, frmSplash splsh, int year)
        {
            InitializeComponent();
            storeNo = sNo;
            splash = splsh;
            currentYear = year;
        }

        private void frmFirstLoad_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < newMap.GetLength(0); i++)
            {
                for (int j = 0; j < newMap.GetLength(1); j++)
                {
                    int.TryParse("0", out newMap[i, j]);
                }
            }

            butLocs.Add(Convert.ToInt16(storeNo));

            for (int i = 0; i < 21; i++)
            {
                butLocs.Add(100);
                butLocs.Add(100);
            }

            progressBar1.Maximum = newMap.Length;

            textBox1.Left = (groupBox1.Width - textBox1.Width) / 2;
            button1.Left = (groupBox1.Width - button1.Width) / 2;

            textBox2.Left = (groupBox2.Width - textBox2.Width) / 2;
            button2.Left = (groupBox2.Width - button2.Width) / 2;

            button3.Left = (this.ClientSize.Width - button3.Width) / 2;


            splash.Opacity = 0.0f;
            this.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open Text File";
            openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog1.Filter = "TXT Files (*.txt)|*.txt";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int count = 0;
                string filename = openFileDialog1.FileName;
                StreamReader reader = new StreamReader(filename);
                textBox1.Text = openFileDialog1.FileName;
                bool error = false;

                string import = reader.ReadLine();

                while ((import = reader.ReadLine()) != null)
                {
                    string[] split = import.Split('^');

                    if (split.Length == 10)
                    {
                        if (split[1] == "STO" + storeNo)
                            count++;
                        else
                        {
                            MessageBox.Show(this, "The Current File Does Not Match Your Store Number");
                            error = true;
                            break;
                        }
                    }
                    else
                    {
                        error = true;
                        break;
                    }
                }

                reader.Close();

                if (error)
                {
                    MessageBox.Show(this, "Error With File, Please Make Sure Delimiter Set To ^");
                    textBox1.Text = "";
                }
                else
                {
                    progressBar3.Maximum = count;
                    button1.Enabled = false;

                    if (button2.Enabled == false)
                        button3.Enabled = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string errorMessage = "";

            openFileDialog2.Title = "Open Text File";
            openFileDialog2.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog2.Filter = "TXT Files (*.txt)|*.txt";
            openFileDialog2.FilterIndex = 2;
            openFileDialog2.RestoreDirectory = true;

            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                int count = 0;
                string filename = openFileDialog2.FileName;
                StreamReader reader = new StreamReader(filename);
                textBox2.Text = openFileDialog2.FileName;
                bool error = false;

                string import = reader.ReadLine();

                while ((import = reader.ReadLine()) != null)
                {
                    string[] split = import.Split('^');

                    if (split.Length == 12)
                    {
                        if (split[0] == "STO" + storeNo)
                            count++;
                        else
                        {
                            MessageBox.Show(this, "The Current File Does Not Match Your Store Number");
                            error = true;
                            errorMessage = "The Current File Does Not Match Your Store Number";
                            break;
                        }
                    }
                    else
                    {
                        error = true;
                        errorMessage = "Error With File. The File May Not Be In The Correct Format";
                        break;
                    }
                }

                reader.Close();

                if (error)
                {
                    MessageBox.Show(this, errorMessage);
                    textBox2.Text = "";
                }
                else
                {
                    progressBar4.Maximum = count;
                    button2.Enabled = false;

                    if (button1.Enabled == false)
                        button3.Enabled = true;
                }
            }
        }

        
        private void button3_Click(object sender, EventArgs e)
        {
            int start = 35;

            for (int i = 0; i < 53; i++)
            {
                List<string> toAdd = new List<string>();

                toAdd.Add(start.ToString());

                if (i <= 17)
                {
                    toAdd.Add("1");
                    toAdd.Add("0");
                }
                else if (i > 17 && i <= 34)
                {
                    toAdd.Add("2");
                    toAdd.Add("0");
                }
                else
                {
                    toAdd.Add("3");
                    toAdd.Add("0");
                }

                toAdd.Add("0");
                toAdd.Add("0");
                toAdd.Add("0");
 
                weeklyInfo.Add(toAdd);

                if (start == 52)
                    start = 1;
                else
                    start++;
            }

            button3.Enabled = false;

            Thread loading = new Thread(load_Information);
            loading.IsBackground = true;
            loading.Start();
        
        }

        public void load_Information()
        {
            databaseAccessability.newStoreButLocs(butLocs);
            mapManagement.saveMap(newMap, storeNo, this);
            databaseAccessability.newStoreSalesInfo(storeNo, this, currentYear);
            databaseAccessability.newStoreWeekTargets(storeNo, weeklyInfo, this, currentYear);

            StreamReader reader = new StreamReader(textBox1.Text);
            String import = reader.ReadLine();

            while ((import = reader.ReadLine()) != null)
            {
                string[] split = import.Split('^');
                databaseAccessability.insertHFB(
                    split[0], split[1], split[2], split[3], split[4], split[5],
                    split[6], split[7], split[8], split[9], this);
            }
            reader.Close();

            StreamReader reader2 = new StreamReader(textBox2.Text);
            string import2 = reader2.ReadLine();

            while ((import2 = reader2.ReadLine()) != null)
            {
                string[] split2 = import2.Split('^');
                databaseAccessability.insertArticles(split2[0], split2[1], split2[3], split2[4], split2[5], split2[6], split2[7], split2[8], split2[9], split2[10],
                    split2[11], this);
            }
            reader2.Close();

            frmSplash splash = new frmSplash();
            this.Invoke(new Action(() => splash.Show()));
            this.Invoke(new Action(() => this.Hide()));

            //MessageBox.Show("Setup Complete!" + Environment.NewLine + "Please Restart The Application", "Restart Required", MessageBoxButtons.OK);
            //this.Invoke(new Action(() => Application.Exit()));
        }

    }
}
